/**
 * 
 */
package t4j.typeinfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import t4j.typeinfo.factory.Factory;


class Part {
	public String toString() {
		return getClass().getSimpleName();
	}

	static List<Factory<? extends Part>> partFactoris = new ArrayList<Factory<? extends Part>>();

	static {
		partFactoris.add(new FuelFilter.Factory());
		partFactoris.add(new AirFilter.Factory());
		partFactoris.add(new CabinAirFilter.Factory());
		partFactoris.add(new OilFilter.Factory());
		partFactoris.add(new FanBelt.Factory());
		partFactoris.add(new PowerSteeringBelt.Factory());
		partFactoris.add(new GeneratorBelt.Factory());
	}
	
	private static Random rand = new Random(47);
	
	public static Part createRandom(){
		int n = rand.nextInt(partFactoris.size());
		
		return partFactoris.get(n).create();
	}
}

//Product 基类
class Filter extends Part {
}

//ConcreteProduct 具体实现类类 
class FuelFilter extends Filter {

	FuelFilter(){
		System.out.println("FuelFilter 被创建");
	}
	
	//实现工厂接口类
	public static class Factory implements t4j.typeinfo.factory.Factory<FuelFilter> {

		Factory(){
			System.out.println("FuelFilter.Factory 被创建");
		}
		
		public FuelFilter create() {
			return new FuelFilter();
		}

	}
}

class AirFilter extends Filter {

	public static class Factory implements t4j.typeinfo.factory.Factory<AirFilter> {

		public AirFilter create() {
			return new AirFilter();
		}

	}
}

class CabinAirFilter extends Filter {
	public static class Factory implements t4j.typeinfo.factory.Factory<CabinAirFilter> {
		public CabinAirFilter create() {
			return new CabinAirFilter();
		}
	}
}

class OilFilter extends Filter {
	public static class Factory implements t4j.typeinfo.factory.Factory<OilFilter> {
		public OilFilter create() {
			return new OilFilter();
		}
	}
}

class Belt extends Part {
}

class FanBelt extends Belt {
	public static class Factory implements t4j.typeinfo.factory.Factory<FanBelt> {
		public FanBelt create() {
			return new FanBelt();
		}
	}
}

class GeneratorBelt extends Belt {
	public static class Factory implements t4j.typeinfo.factory.Factory<GeneratorBelt> {
		public GeneratorBelt create() {
			return new GeneratorBelt();
		}
	}
}

class PowerSteeringBelt extends Belt {
	public static class Factory implements t4j.typeinfo.factory.Factory<PowerSteeringBelt> {
		public PowerSteeringBelt create() {
			return new PowerSteeringBelt();
		}
	}
}

public class RegisteredFactories {
 public static void main(String[] args) {
	for(int i = 0 ; i< 10 ; i++)
		System.out.println(Part.createRandom());
}
}
